package com.spark.quartz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spark.common.annotation.DictTranslate;
import com.spark.common.constants.validGroup.AddGroup;
import com.spark.common.constants.validGroup.EditGroup;
import com.spark.common.core.Result;
import com.spark.common.query.QueryGenerator;
import com.spark.quartz.entity.QrtzJob;
import com.spark.quartz.service.IQrtzJobService;
import com.spark.quartz.utils.JobScheduleManager;
import org.quartz.SchedulerException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 定时任务Controller
 *
 * @author LYCHEE
 * @date 2025/04/01 15:18
 */
@RestController
@RequestMapping(value = "/quartz/qrtzJob")
public class QrtzJobController {

    @Resource
    private IQrtzJobService qrtzJobService;
    @Resource
    private JobScheduleManager jobScheduleManager;

    /**
     * 分页列表查询
     *
     * @param qrtzJob 定时任务对象
     * @param pageNo   分页
     * @param pageSize 分页
     * @return 定时任务列表
     */
    @DictTranslate
    @PreAuthorize("@sp.hasPerm('quartz:qrtzJob:query')")
    @GetMapping(value = "/queryPageList")
    public Result<?> queryPageList(QrtzJob qrtzJob,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        QueryWrapper<QrtzJob> queryWrapper = QueryGenerator.initQueryWrapper(qrtzJob);
        Page<QrtzJob> page = new Page<QrtzJob>(pageNo, pageSize);
        IPage<QrtzJob> pageList = qrtzJobService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 列表查询
     *
     * @param qrtzJob 定时任务对象
     * @return 定时任务列表
     */
    @DictTranslate
    @PreAuthorize("@sp.hasPerm('quartz:qrtzJob:query')")
    @GetMapping(value = "/queryList")
    public Result<?> queryList(QrtzJob qrtzJob) {
        QueryWrapper<QrtzJob> queryWrapper = QueryGenerator.initQueryWrapper(qrtzJob);
        List<QrtzJob> list = qrtzJobService.list(queryWrapper);
        return Result.ok(list);
    }

    /**
     * 通过ID查询
     *
     * @param id 主键
     * @return 定时任务对象
     */
    @DictTranslate
    @PreAuthorize("@sp.hasPerm('quartz:qrtzJob:queryById')")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam String id) {
        QrtzJob qrtzJob = qrtzJobService.getById(id);
        if (Objects.isNull(qrtzJob)) {
            return Result.fail("数据获取失败");
        }
        return Result.ok(qrtzJob);
    }

    /**
     * 新增
     *
     * @param qrtzJob 定时任务对象
     * @return 成功或失败
     */
    @PreAuthorize("@sp.hasPerm('quartz:qrtzJob:add')")
    @PostMapping(value = "/add")
    public Result<?> add(@Validated(AddGroup.class) @RequestBody QrtzJob qrtzJob) throws SchedulerException {
        boolean save = qrtzJobService.save(qrtzJob);
        jobScheduleManager.scheduleJob(qrtzJob);
        return Result.flag(save);
    }

    /**
     * 编辑
     *
     * @param qrtzJob 定时任务对象
     * @return 成功或失败
     */
    @PreAuthorize("@sp.hasPerm('quartz:qrtzJob:edit')")
    @PostMapping(value = "/edit")
    public Result<?> edit(@Validated(EditGroup.class) @RequestBody QrtzJob qrtzJob) throws SchedulerException {
        boolean update = qrtzJobService.updateById(qrtzJob);
        jobScheduleManager.scheduleJob(qrtzJob);
        return Result.flag(update);
    }

    /**
     * 通过id删除
     *
     * @param id 主键
     * @return 成功或失败
     */
    @PreAuthorize("@sp.hasPerm('quartz:qrtzJob:del')")
    @DeleteMapping(value = "/delete/{id}")
    public Result<?> delete(@PathVariable(name = "id") String id) throws SchedulerException {
        jobScheduleManager.deleteJob(id);
        boolean update = qrtzJobService.removeById(id);
        return Result.flag(update);
    }

    /**
     * 通过id批量删除删除
     *
     * @param ids 主键
     * @return 成功或失败
     */
    @PreAuthorize("@sp.hasPerm('quartz:qrtzJob:batchDel')")
    @DeleteMapping(value = "/deleteBatchByIds/{ids}")
    public Result<?> deleteBatchByIds(@PathVariable(name = "ids") String[] ids) {
        boolean update = qrtzJobService.removeBatchByIds(Arrays.asList(ids));
        return Result.flag(update);
    }

}
